﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;

[AddComponentMenu("PUEX/Affector/Size/DimensionsOverLifetime")]
public class PUEXDimensionsOverLifetimeAffector : PUEXAffector {

	#region - Editable property
	public PUEXDynamicAttribute dimensionsXOverLifetime;
	public PUEXDynamicAttribute dimensionsYOverLifetime;
	public PUEXDynamicAttribute dimensionsZOverLifetime;
	public bool useEmitterTime;
	#endregion
	
	#region - Lifecycle
	public PUEXDimensionsOverLifetimeAffector ()
	{
		dimensionsXOverLifetime = PUEXDynamicAttribute.GenerateFixed (1f);
		dimensionsYOverLifetime = PUEXDynamicAttribute.GenerateFixed (1f);
		dimensionsZOverLifetime = PUEXDynamicAttribute.GenerateFixed (1f);
		useEmitterTime = false;
	}
	#endregion
	
	#region - Update
	public override void update (float dt, PUEXParticle particle)
	{
		float t = particle.timeFraction;
		if (useEmitterTime)
			t = inTrigger() ? triggerTimeFraction : ownerEmitter.timeFraction;

		particle.setDimensions (Vector3.Scale (particle.originalDimensions(), new Vector3(dimensionsXOverLifetime.getValue(t), 
		                                                            dimensionsYOverLifetime.getValue(t), 
		                                                            dimensionsZOverLifetime.getValue(t))));
	}
	#endregion

	#region - Param property
	public override void updateParams (PUEXParticleSystem system)
	{
		dimensionsXOverLifetime.parameterize (system);
		dimensionsYOverLifetime.parameterize (system);
		dimensionsZOverLifetime.parameterize (system);
	}
	#endregion

	#region - Serialization
	public override JsonData save (JsonData emitterData, JsonData systemData)
	{
		JsonData ret = base.save (emitterData, systemData);
		
		ret ["dimensions_x_over_lifetime"] = PUEXUtils.serializeDynamicAttribute(dimensionsXOverLifetime);
		ret ["dimensions_y_over_lifetime"] = PUEXUtils.serializeDynamicAttribute(dimensionsYOverLifetime);
		ret ["dimensions_z_over_lifetime"] = PUEXUtils.serializeDynamicAttribute(dimensionsZOverLifetime);
		ret ["use_emitter_time"] = useEmitterTime;
		
		return ret;
	}
	#endregion

}
